unit Unit3;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, ExtCtrls, ColorGrd, ComCtrls, Spin, Buttons;

type
  TForm3 = class(TForm)
    Button1: TButton;
    //PaintBox1:T
    Panel1: TPanel;
    GroupBox1: TGroupBox;
    GroupBox2: TGroupBox;
    TrackBar2: TTrackBar;
    SpeedButton1: TSpeedButton;
    SpeedButton2: TSpeedButton;
    SpeedButton3: TSpeedButton;
    SpeedButton4: TSpeedButton;
    PaintBox1: TPaintBox;
    SpeedButton5: TSpeedButton;
    SpeedButton6: TSpeedButton;
    Label1: TLabel;
    Label2: TLabel;
    Edit1: TEdit;
    Edit2: TEdit;
    procedure Button1Click(Sender: TObject);
    procedure grafico;
    procedure SpeedButton2Click(Sender: TObject);
    procedure SpeedButton4Click(Sender: TObject);
    procedure SpeedButton3Click(Sender: TObject);
    procedure SpeedButton1Click(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure SpeedButton5Click(Sender: TObject);
    procedure SpeedButton6Click(Sender: TObject);

//    procedure TrackBar1Change(Sender: TObject);

  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form3: TForm3;
  centrox,centroy,zoom,meiox,meioy,x1,x2,y1,y2:integer;
  res:real;
implementation

uses Unit4,Ucompute, Unit2,funcao;

{$R *.DFM}


procedure TForm3.Button1Click(Sender: TObject);
begin
    grafico;
end;

procedure TForm3.grafico;
var
       i,k,p:integer;
       x,y,z,j:real;
       erro1,erro2,erro3:boolean;
       g_x, h_x:Tfuncao;
     {  y2_str:str100;}
begin
  form3.PaintBox1.Canvas.Rectangle(x1-1,y1-1,x2+1,y2+1);
     case trackbar2.Position of
        1: res:=1;
        2: res:=0.1;
        3: res:=0.01;
     end;
     i:=centrox{gx_l div 2};
     while i<=x2 do
      begin
        for k:=y1 to y2 do
           paintbox1.Canvas.Pixels[i,k]:=clGray;
        i:=i+zoom;
      end;
      i:=centrox;
      while i>=x1 do
       begin
         for k:=y1 to y2 do
            paintbox1.Canvas.Pixels[i,k]:=clGray;
         i:= i-zoom;
       end;
      i:=centroy{gy_l div 2};
      while i<=y2 do
       begin
          for k:=x1 to x2 do
             paintbox1.Canvas.Pixels[k,i]:=clGray;
          i:= i+zoom;
       end;
      i:=centroy;
      while i>=y1 do
       begin
         for k:=x1 to x2 do
            paintbox1.Canvas.Pixels[k,i]:=clGray;
         i:=i-zoom;
       end;
     if (centrox>=x1) and (centrox<=x2) then
        for k:=y1 to y2 do
            paintbox1.Canvas.Pixels[centrox,k]:=clred;
     if (centroy>=y1) and (centroy<=y2) then
        for k:=x1 to x2 do
            paintbox1.Canvas.Pixels[k,centroy]:=clred;
     {desenha a funcao}
     j:=x1;           { limita o desenho no eixo x }
     g_x.funcao;
     h_x.funcao;
     g_x.inicializa(form2.edit1.text);
     h_x.inicializa(form2.edit2.text);
     while j <= x2 do
      begin
         x:=(j-centrox) / zoom;
         j:=j+res;
        y:= {g_x.calcula(x,erro2);} compute(p,form2.edit1.text,x,erro1,erro2);
         z:= {h_x.calcula(x,erro3);}compute(p,form2.edit2.text,x,erro1,erro3);
         if not(erro2) then
            if (centroy-y*zoom >= y1)and(centroy-y*zoom <= y2) then
             begin  {^ limita o desenho no eixo y }
 {INTEGRAL}   { linha(round(j),round(centroy-y*zoom),round(j),centroy,2);}
                paintbox1.Canvas.Pixels[round(j),round(centroy-y*zoom)]:=cllime;
     //         putpixel(round(j),round(centroy-y*zoom),2);
             end;
         if not(erro3) then
           if (centroy-z*zoom >= y1) and (centroy-z*zoom <= y2) then
              paintbox1.Canvas.Pixels[round(j),round(centroy-z*zoom)]:=clyellow;
       end; {
     setcolor(11);
     str((centroy-y2)/zoom:0:2,y2_str);
     escreva(meiox-15,y2-10,y2_str);
     str((centroy-y1)/zoom:0:2,y2_str);
     escreva(meiox-15,y1-2,y2_str);
     SetTextStyle(2,1,4);
     SetTextJustify(1,1);
     str((x2-centrox)/zoom:0:2,y2_str);
     OutTextXY(x2-7,meioy,y2_str);
     str((x1-centrox)/zoom:0:2,y2_str);
     OutTextXY(x1+1,meioy,y2_str);

     mouseon;
     repeat
        op:=upcase(readkey);
     until op in [#8,#13,'S','D','E','R','F'];


     if op='D' then

     if op='R' then

     if op='F' then

     if op='E' then

  {until op='S';  }
   g_x.destroy;
   h_x.destroy;
end;

{procedure TForm3.TrackBar1Change(Sender: TObject);
begin
    grafico;
end;}

procedure TForm3.SpeedButton2Click(Sender: TObject);
begin
    centroy:=centroy+zoom;
    grafico;
end;

procedure TForm3.SpeedButton4Click(Sender: TObject);
begin
    centroy:=centroy-zoom;
    grafico;
end;

procedure TForm3.SpeedButton3Click(Sender: TObject);
begin
    centrox:=centrox-zoom;
    grafico;
end;

procedure TForm3.SpeedButton1Click(Sender: TObject);
begin
      centrox:=centrox+zoom;
      grafico;
end;

procedure TForm3.FormShow(Sender: TObject);
begin
    x1:=0;   x2:=399;
    y1:=0;   y2:=399;
    meiox:=(x2+x1) div 2;
    meioy:=(y2+y1) div 2;
    centrox := meiox;
    centroy := meioy;
    zoom:=16;
    res:=1;
    Edit1.Text:=Form2.Edit1.text;
    Edit2.Text:=Form2.Edit2.text;
end;

procedure TForm3.SpeedButton5Click(Sender: TObject);
begin
    if (zoom>4) then
      begin
        centrox:=centrox - (centrox-meiox) div 2;
        centroy:=centroy - (centroy-meioy) div 2;
        zoom:=zoom div 2;
        grafico;
      end;
end;

procedure TForm3.SpeedButton6Click(Sender: TObject);
begin
    if (zoom<128) then
      begin
        centrox:=centrox + (centrox-meiox);
        centroy:=centroy + (centroy-meioy);
        zoom:=zoom * 2{+4;};
        grafico;
     end;
end;

end.
